package com.dc.service.impl;

import com.dc.base.model.BaseModel;
import com.dc.dao.UserDao;
import com.dc.dao.impl.UserDaoImpl;
import com.dc.service.impl.UserService;
import java.util.List;

/**
 * Created by Acer on 2019/9/28.
 */
public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();
    @Override
    public BaseModel addUser(Object[] params) throws Exception {
        BaseModel baseModel = new BaseModel();
        String sql ="insert into t_user(user_name,user_trueName,user_cardId,user_email,user_telephone,user_qqId,user_pwd,regTime,question,answer,state) values(?,?,?,?,?,?,?,?,?,?,?)";
        int result = userDao.addUser(sql,params);
        if(result>0){
            baseModel.setCode(1);
            baseModel.setMessage("添加成功");
        }else {
            baseModel.setCode(0);
            baseModel.setMessage("添加失败");
        }
        return baseModel;
    }

    @Override
    public BaseModel updateUser(Object[] params) throws Exception {
        String sql="update t_user set user_name=?,user_trueName=?,user_cardId=?,user_email=?,user_telephone=?,user_qqId=?,user_pwd=?,regTime=?,question=?,answer=?,state=?";
        int result = userDao.updateUser(sql,params);
        BaseModel baseModel = new BaseModel();

        if(result>0){
            baseModel.setCode(1);
            baseModel.setMessage("添加成功");
        }else {
            baseModel.setCode(0);
            baseModel.setMessage("添加失败");
        }
        return baseModel;

    }

    @Override
    public BaseModel deleteUser(int id) throws Exception {
        BaseModel baseModel = new BaseModel();
        System.out.println(id);
        String sql = "delete from t_user where user_id = "+id+"";
        int result = userDao.deleteUser(sql);
        if(result>0){
            baseModel.setCode(1);
            baseModel.setMessage("删除成功");
        }else {
            baseModel.setCode(0);
            baseModel.setMessage("删除失败");
        }
        return baseModel;
    }

    @Override
    public BaseModel selectUser(Object[] params) throws Exception {
        BaseModel baseModel=new BaseModel();
        String sql="select * from t_user";
        List lists=userDao.selectUser(sql,null);
        baseModel.setCode(1);
        baseModel.setMessage("查询成功");
        baseModel.setData(lists);
        return baseModel;
    }

    @Override
    public BaseModel selectUserid(int id) throws Exception {
        BaseModel baseModel=new BaseModel();
        String sql="select * from t_user where user_id="+id+"";
        List lists=userDao.selectUserid(sql);
        baseModel.setCode(1);
        baseModel.setMessage("查询成功");
        baseModel.setData(lists);
        return baseModel;
    }
}
